// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import SkillEntity from "../../Models/Skills/SkillEntity";
import SkillManager from "../../Models/Skills/SkillManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SkillItem extends cc.Component {

    @property(cc.Label)
    nameTxt: cc.Label = null;
    @property(cc.Label)
    lvTxt: cc.Label = null;
    @property(cc.Label)
    describeTxt: cc.Label = null;
    @property(cc.Button)
    unlockBtn: cc.Button = null;
    @property(cc.Button)
    upgradeBtn: cc.Button = null;
    @property(cc.Label)
    unlockTxt: cc.Label = null;
    @property(cc.Label)
    upgradeTxt: cc.Label = null;

    skillEy: SkillEntity = null;
    skillid: number;


    drawItem(id: number) {
        this.skillid = id;
        this.skillEy = SkillManager.single.getSkillEntity(this.skillid);
        var skillTp = SkillManager.single.getSkillTemplate(this.skillid);
        this.nameTxt.string = skillTp.skillName;

        if (this.skillEy != null) {
            this.unlockBtn.node.active = false;
            this.upgradeBtn.node.active = true;
            this.lvTxt.string = this.skillEy.level + "级";
            this.upgradeTxt.string = "" + this.skillEy.upgradeGoldNum;
            this.describeTxt.string = skillTp.skillDescribe.replace("@", this.skillEy.valueNum.toFixed(2));
            this.upgradeBtn.interactable = SaveManager.single.isEnoughGold(this.skillEy.upgradeGoldNum);
        } else {
            this.lvTxt.string = "0级";
            this.unlockBtn.node.active = true;
            this.upgradeBtn.node.active = false;
            this.unlockTxt.string = "" + skillTp.unlockCost;
            this.describeTxt.string = skillTp.skillDescribe.replace("@", "0");
            this.unlockBtn.interactable = SaveManager.single.isEnoughGold(skillTp.unlockCost);
        }

    }

    unlockClick() {

        var skillTp = SkillManager.single.getSkillTemplate(this.skillid);
        if (!SaveManager.single.isEnoughGold(skillTp.unlockCost)) {
            UIManager.single.showTips("金币不足。");
            return;
        }
        SkillManager.single.requestUnlockSkill(this.skillid);
    }

    upgradeClick() {
        var userdata = SaveManager.single.userdata;
        var skillEy = userdata.getSkillEntity(this.skillid);
        if (skillEy.level >= skillEy.skillTp.maxLevel) {
            UIManager.single.showTips("技能已满级。");
            return;
        }
        if (!SaveManager.single.isEnoughGold(skillEy.upgradeGoldNum)) {
            UIManager.single.showTips("金币不足。");
            return;
        }

        SkillManager.single.requestUpgradeSkill(this.skillid);
    }
}
